//
//  SideSlipSourceTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/8.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SideSlipSourceTableViewCell.h"
#import "FilterCommonCollectionViewCell.h"
#import "ZYSideSlipFilterRegionModel.h"
#import "CommonItemModel.h"

@interface SideSlipSourceTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UILabel *titleL;
@property (strong, nonatomic) UICollectionView *mainCollectionView;

@property (strong, nonatomic) ZYSideSlipFilterRegionModel *regionModel;

@property (strong, nonatomic) NSArray *collectionArr;
@property (strong, nonatomic) NSMutableArray *selectedItemList;


@end

@implementation SideSlipSourceTableViewCell

+ (NSString *)cellReuseIdentifier {
    return @"SideSlipSourceTableViewCell";
}

+ (instancetype)createCellWithIndexPath:(NSIndexPath *)indexPath {

    SideSlipSourceTableViewCell *cell = [[NSClassFromString(@"SideSlipSourceTableViewCell") alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SideSlipSourceTableViewCell"] ;

    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self configureCell];
        
    }
    
    return self;
}
- (void)configureCell {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.titleL];
    [self.contentView addSubview:self.mainCollectionView];
    
    self.titleL.frame = CGRectMake(jisuan(20), jisuan(30), jisuan(100), jisuan(36));
    self.mainCollectionView.frame = CGRectMake(0, jisuan(86), jisuan(670), jisuan(60));

}

- (void)updateCellWithModel:(ZYSideSlipFilterRegionModel *__autoreleasing *)model
                  indexPath:(NSIndexPath *)indexPath {
    self.regionModel = *model;
    //title
    [self.titleL setText:_regionModel.regionTitle];
    //content
    self.collectionArr = _regionModel.itemList;
    //UI
    [self fitCollectonViewHeight];
    [_mainCollectionView reloadData];
    
    CGFloat height = CGRectGetMaxY(self.mainCollectionView.frame) + jisuan(30);
    
//    NSLog(@"%.2f",CGRectGetMaxY(self.mainCollectionView.frame));
    
    if (self.cellHeigthBlock) {
        
        self.cellHeigthBlock(height);
    }
}
//根据数据源个数决定collectionView高度
- (void)fitCollectonViewHeight {
    CGFloat displayNumOfRow;
    
    NSInteger count = self.collectionArr.count % 3;
    NSInteger row = self.collectionArr.count / 3;
    
    if (count == 0) {
        
        displayNumOfRow = row;
        
    }else {
        
        displayNumOfRow = row + 1;
    }
    

    CGFloat collectionViewHeight = displayNumOfRow * jisuan(60) + (displayNumOfRow - 1) * jisuan(20);
    
//    NSLog(@"%@", NSStringFromCGRect(self.mainCollectionView.frame));
    self.mainCollectionView.height = collectionViewHeight;
    [self.mainCollectionView updateHeight:collectionViewHeight];
//    NSLog(@"====%@", NSStringFromCGRect(self.mainCollectionView.frame));
}

- (BOOL)tap2SelectItem:(NSIndexPath *)indexPath {
    NSArray *itemArray = _regionModel.itemList;
    CommonItemModel *model = [itemArray objectAtIndex:indexPath.row];
    model.selected = !model.selected;
    [self updateSelectedItemListWithItem:model];
    return model.selected;
}

- (void)updateSelectedItemListWithItem:(CommonItemModel *)model {
    if (model.selected) {
        [self.selectedItemList addObject:model];
    } else {
        [self.selectedItemList removeObject:model];
    }
    //update Data
    _regionModel.selectedItemList = _selectedItemList;
}

#pragma mark -collectionView delegate \ datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.collectionArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FilterCommonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[FilterCommonCollectionViewCell cellReuseIdentifier] forIndexPath:indexPath];
    CommonItemModel *model = [self.collectionArr objectAtIndex:indexPath.row];
    [cell updateCellWithModel:model];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    [self tap2SelectItem:indexPath];
    [_mainCollectionView reloadData];
}

#pragma mark -lazy

- (UICollectionView *)mainCollectionView {
    
    if (!_mainCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(196), jisuan(60));
        flowLayout.minimumLineSpacing = jisuan(20);
        flowLayout.minimumInteritemSpacing = jisuan(20);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, jisuan(20), jisuan(30), jisuan(20));
        
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        
        _mainCollectionView.backgroundColor = WhiteColor;
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        
        _mainCollectionView.scrollEnabled = NO;
        
        _mainCollectionView.showsVerticalScrollIndicator = NO;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_mainCollectionView registerClass:[FilterCommonCollectionViewCell class] forCellWithReuseIdentifier:[FilterCommonCollectionViewCell cellReuseIdentifier]];
        
        
        if (@available(iOS 11.0, *)) {
            _mainCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _mainCollectionView;
}
- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.font = Font(24);
        _titleL.textColor = Color333333;
        _titleL.textAlignment = NSTextAlignmentLeft;
        
    }
    return _titleL;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
